//
//  RTJTheContractController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/18.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJTheContractController.h"
#import <WebKit/WebKit.h>

@interface RTJTheContractController ()<UIWebViewDelegate,WKNavigationDelegate>

@property (strong, nonatomic)WKWebView *pdfWebView;

@end

@implementation RTJTheContractController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"合同详情";
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    // 自适应屏幕宽度js
    NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    
    // 添加自适应屏幕宽度js调用的方法
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUserScript];
    wkWebConfig.userContentController = wkUController;
    self.pdfWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH) configuration:wkWebConfig];
    self.pdfWebView.navigationDelegate = self;
    self.pdfWebView.scrollView.backgroundColor = [UIColor whiteColor];
    [SVProgressHUD show];
   
    [self loadData];
    
    [self.view addSubview:self.pdfWebView];
    
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    
    [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
    
    [RTJNetWork asyncRequestWithURL:@"user/get_contract_pdf_for_zq" params:@{@"contract_no":_contractStr} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        [self.view removeEmptyView];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                NSString* encodedString = [data[@"data"][@"contract_url"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                
                NSString *Path = [Apputil getfilePathWithUrlStr:encodedString docname:@"合同详情"];
                NSFileManager* maneger = [NSFileManager defaultManager];
                
                if ([maneger fileExistsAtPath:Path]) {
                    
                    NSURL *url = [NSURL fileURLWithPath:Path];
                    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
                    [self.pdfWebView loadRequest:request];
                    
                }else{
                    
                    NSURL* url = [NSURL URLWithString:encodedString];
                    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
                    [self.pdfWebView loadRequest:request];
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                
                [self loadData];
            }];
        }
        
    }];
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [SVProgressHUD dismiss];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [SVProgressHUD dismiss];
}

#pragma mark - WKNavigationDelegate
/// 2 页面开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    
}
/// 4 开始获取到网页内容时返回
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    
}
/// 5 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [SVProgressHUD dismiss];
}

/// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    [SVProgressHUD dismiss];
}

@end
